package com.hmjk.health.views;

import java.util.ArrayList;

import com.hmjk.health.R;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class RightSidePopMenu implements OnItemClickListener{
	private View popMenuView;
	private PopupWindow window;
	private ArrayList<Item> mItems = null;
	private PopMenuListAdapter mAdapter = null;
	private OnPopMenuItemClickListener mClickListener= null;
	
	public interface OnPopMenuItemClickListener{
		public boolean onPopMenuItemClick(int ID);
	}
	
	public static class Item{
		int icon;
		int text;
		int ID;
		String stext;
		
		public Item(int icon, int text, int ID){
			this.icon = icon;
			this.text = text;
			this.ID = ID;
		}
		
		public Item(int icon, String stext, int ID){
			this.icon = icon;
			this.stext = stext;
			this.ID = ID;
		}
	}
	
	class PopMenuListAdapter extends BaseAdapter{
		private Context context;
		private class ViewHolder{
			TextView text;
			ImageView icon;
			int ID;
		}
		
		PopMenuListAdapter(Context context){
			this.context = context;
		}
		
		@Override
		public int getCount() {
			if(mItems != null)
				return mItems.size();
			
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int pos, View itemView, ViewGroup group) {
			ViewHolder h;
			if(itemView == null){
				LayoutInflater inflater = LayoutInflater.from(context);
				itemView = inflater.inflate(R.layout.pop_menu_item, null);
				h = new ViewHolder();
				h.icon = (ImageView)itemView.findViewById(R.id.popMenuIcon);
				h.text = (TextView)itemView.findViewById(R.id.popMenuText);
				itemView.setTag(h);
			}else{
				h = (ViewHolder) itemView.getTag();
			}
			
			Item i = mItems.get(pos);
			if(i != null){
				h.icon.setImageResource(i.icon);
				if(i.stext != null){
					h.text.setText(i.stext);
				}else{
					h.text.setText(i.text);
				}
				h.ID = i.ID;
			}
			return itemView;
		}
		
	}
	
	public RightSidePopMenu(Context context, ArrayList<Item> data){
		LayoutInflater inflater = LayoutInflater.from(context);
		
		popMenuView = inflater.inflate(R.layout.pop_menu_view, null);
		ListView list = (ListView) popMenuView.findViewById(R.id.popMenuList);
		window = new PopupWindow(popMenuView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
		
		mItems = data;
		mAdapter = new PopMenuListAdapter(context);
		list.setAdapter(mAdapter);
		list.setOnItemClickListener(this);
		window.setTouchable(true);
		
		window.setBackgroundDrawable(new ColorDrawable(0x00000000));
	}
	
	public void setBackground(int bg_res){
		popMenuView.setBackgroundResource(bg_res);
	}
	
	public void show(View view){
		window.showAsDropDown(view);
	}
	
	public void dismiss(){
		if((window != null) && window.isShowing())
			window.dismiss();
	}
	
	public void setOnPopMenuItemClickListener(OnPopMenuItemClickListener l){
		mClickListener = l;
	}

	@Override
	public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
		Item i = mItems.get(pos);
		if(mClickListener != null)
			mClickListener.onPopMenuItemClick(i.ID);
		
		window.dismiss();
	}
}
